import http from './http' // 导入已配置的 axios 实例

// GET请求：获取博客列表
export async function getBlogList<T>(opt = {}): Promise<T> {
  const { page = 1, size = 10, category = '', keyword = '' } = opt
  const url = `/api/blogs?page=${page.value}&size=${size.value}&category=${category.value}&keyword=${keyword.value}`
  try {
    return await http.get<T>(url)
  } catch (error) {
    throw error
  }
}

// GET请求：获取博客详情
export async function getBlogDetails<T>(blogId: string): Promise<T> {
  try {
    return await http.get<T>(`/api/blogs/${blogId}`)
  } catch (error) {
    throw error
  }
}

// POST请求：创建博客
export async function createBlog<T>(data: Record<string, any>): Promise<T> {
  try {
    return await http.post<T>('/api/blogs', data)
  } catch (error) {
    throw error
  }
}

// PUT请求：编辑博客
export async function editBlog<T>(
  blogId: string,
  data: Record<string, any>
): Promise<T> {
  try {
    return await http.put<T>(`/api/blogs/${blogId}`, data)
  } catch (error) {
    throw error
  }
}

// DELETE请求：删除博客
export async function deleteBlog<T>(blogId: string): Promise<T> {
  try {
    return await http.delete<T>(`/api/blogs/${blogId}`)
  } catch (error) {
    throw error
  }
}

// 其他博客相关请求可以继续在此文件中添加

// 点赞博客
export async function likeBlog<T>(blogId: string): Promise<T> {
  try {
    return await http.post<T>(`/api/blogs/${blogId}/like`)
  } catch (error) {
    throw error
  }
}

// 取消点赞
export async function unlikeBlog<T>(blogId: string): Promise<T> {
  try {
    return await http.delete<T>(`/api/blogs/${blogId}/like`)
  } catch (error) {
    throw error
  }
}

// 收藏博客
export async function collectBlog<T>(blogId: string): Promise<T> {
  try {
    return await http.post<T>(`/api/blogs/${blogId}/favorite`)
  } catch (error) {
    throw error
  }
}

// 取消收藏
export async function unCollectBlog<T>(blogId: string): Promise<T> {
  try {
    return await http.delete<T>(`/api/blogs/${blogId}/favorite`)
  } catch (error) {
    throw error
  }
}
